package com.xbh.wificonfiglib;

import android.content.Context;
import android.text.TextUtils;

import com.xbh.wificonfiglib.common.Constant;
import com.xbh.wificonfiglib.protocol.TouchData;
import com.xbh.wificonfiglib.security.ITouchEncryptor;
import com.xbh.wificonfiglib.task.WifiConfigLibTaskParameter;
import com.xbh.wificonfiglib.task.__WifiConfigLibTaskByTCPIP;
import com.xbh.wificonfiglib.util.TouchNetUtil;

import java.util.List;

public class WifiConfigLibTaskByTCPIP implements IWifiConfigLibTask {
    private __WifiConfigLibTaskByTCPIP _mEsptouchTask;
    private WifiConfigLibTaskParameter _mParameter;

    /**
     * Constructor of EsptouchTask
     *
     * @param apSsid     the Ap's ssid
     * @param apBssid    the Ap's bssid
     * @param apPassword the Ap's password
     * @param context    the {@link Context} of the Application
     */
    public WifiConfigLibTaskByTCPIP(String apSsid, String apBssid, String apPassword, String ip, int port, Context context) {
        this(apSsid, apBssid, apPassword, false,ip, port, null, context);
    }

    /**
     * Constructor of EsptouchTask
     *
     * @param apSsid     the Ap's ssid
     * @param apBssid    the Ap's bssid
     * @param apPassword the Ap's password
     * @param context    the {@link Context} of the Application
     */
    public WifiConfigLibTaskByTCPIP(byte[] apSsid, byte[] apBssid, byte[] apPassword, String ip, int port, Context context) {
        this(apSsid, apBssid, apPassword,false, ip, port, null, context);
    }
    public WifiConfigLibTaskByTCPIP(byte[] apSsid, byte[] apBssid, byte[] apPassword,
                                    boolean ssidWithZh,  String ip, int port, Context context) {
        this(apSsid, apBssid,  apPassword, ssidWithZh, ip, port, null, context);
    }

    private WifiConfigLibTaskByTCPIP(String apSsid, String apBssid, String apPassword,
                                     boolean ssidWithZh, String ip, int port, ITouchEncryptor encryptor, Context context) {
        if (TextUtils.isEmpty(apSsid)) {
            throw new NullPointerException("SSID can't be empty");
        }
        if (TextUtils.isEmpty(apBssid)) {
            throw new NullPointerException("BSSID can't be empty");
        }
        if (apPassword == null) {
            apPassword = "";
        }
        TouchData ssid = new TouchData(apSsid);
        TouchData bssid = new TouchData(TouchNetUtil.parseBssid2bytes(apBssid));
        if (bssid.getData().length != 6) {
            throw new IllegalArgumentException("Bssid format must be aa:bb:cc:dd:ee:ff");
        }
        TouchData password = new TouchData(apPassword);
        init(context, ssid, bssid, password,ssidWithZh, ip, port, encryptor);
    }

    private WifiConfigLibTaskByTCPIP(byte[] apSsid, byte[] apBssid, byte[] apPassword,
                                     boolean ssidWithZh, String ip, int port, ITouchEncryptor encryptor, Context context) {
        if (apSsid == null || apSsid.length == 0) {
            throw new NullPointerException("SSID can't be empty");
        }
        // modify on 2021/02/23 by lei
        // if (apBssid == null || apBssid.length != 6) {
        //     throw new NullPointerException("BSSID is empty or length is not 6");
        // }
        if (apPassword == null) {
            apPassword = new byte[0];
        }
        TouchData ssid = new TouchData(apSsid);
        TouchData bssid = new TouchData(apBssid);
        TouchData password = new TouchData(apPassword);
        init(context, ssid, bssid, password, ssidWithZh, ip, port, encryptor);
    }

    private void init(Context context, TouchData ssid, TouchData bssid, TouchData password,
                      boolean ssidWithZh, String ip, int port, ITouchEncryptor encryptor) {
        _mParameter = new WifiConfigLibTaskParameter();
        _mParameter.setEsptouchResultTotalLen(ssid.getData().length + password.getData().length + bssid.getData().length + Constant.HEADER_LEN);
        _mEsptouchTask = new __WifiConfigLibTaskByTCPIP(context, ssid, bssid, password, ip, port, encryptor, _mParameter);
        _mEsptouchTask.setSsidWithZh(ssidWithZh);
    }

    @Override
    public void interrupt() {
        _mEsptouchTask.interrupt();
    }

    @Override
    public IWifiConfigLibResult executeForResult() throws RuntimeException {
        return _mEsptouchTask.executeForResult();
    }

    @Override
    public boolean isCancelled() {
        return _mEsptouchTask.isCancelled();
    }

    @Override
    public List<IWifiConfigLibResult> executeForResults(int expectTaskResultCount)
            throws RuntimeException {
        if (expectTaskResultCount <= 0) {
            expectTaskResultCount = Integer.MAX_VALUE;
        }
        return _mEsptouchTask.executeForResults(expectTaskResultCount);
    }

    @Override
    public void setEsptouchListener(IWifiConfigLibListener esptouchListener) {
        _mEsptouchTask.setEsptouchListener(esptouchListener);
    }

    @Override
    public void setPackageBroadcast(boolean broadcast) {
        _mParameter.setBroadcast(broadcast);
    }
}
